package crud;

import javax.persistence.EntityManager;

import entities.ExamineDetail;
import entities.MedicineDetail;
import entities.MedicineInfo;

public class MedicineDetailCRUD extends AbstractGRUD<MedicineDetail> {
	public MedicineDetailCRUD() {
		super(MedicineDetail.class);
	}

	public String create(String dose, int idMedicineInfo, int quantity, int idExamine) {
		EntityManager em = this.getEm();
		String result = "success";
		try {
			em.getTransaction().begin();
			MedicineInfo medicineInfo = new MedicineInfoCRUD().readById(idMedicineInfo);
			ExamineDetail examineDetail = new ExamineDetailCRUD().readById(idExamine);
			MedicineDetail medicineDetail = new MedicineDetail(dose, medicineInfo, quantity, examineDetail);
			em.persist(medicineDetail);
			em.getTransaction().commit();
		} catch (Exception e) {
			result = e.getMessage();
		}
		return result;
	}

	// Update MedicineDetail danh cho doctor
	public String update(int idMedicineDetail, String dose, int quantity) {
		EntityManager em = this.getEm();
		String rslt = "success";
		try {
			em.getTransaction().begin();
			MedicineDetail medicineDetail = this.readById(idMedicineDetail);
			medicineDetail.setDose(dose);
			medicineDetail.setQuantity(quantity);
			em.merge(medicineDetail);
			em.getTransaction().commit();
		} catch (Exception e) {
			rslt = e.getMessage();
		}
		return rslt;
	}

}
